//
//  HJWelcomeViewController.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/22.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class HJWelcomeViewController: UIViewController {
    // MARK: - 属性
    var iconBottomConstraint: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
        if let urlString = HJUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: urlString)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        HJUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                HJPrint(msg: "error = \(error)")
                return
            }
            if let urlString = HJUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
                let url = NSURL(string: urlString)!
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }

    }
    // MARK: - PrepareUI
    private func prepareUI() {
        // 1.Add child control
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        // 2.Setting layout
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        
        backgroundImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconBottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconBottomConstraint!)
        
        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
    }
    // MAKE: - Animation
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    // moveAnimation
    private func moveAnimation() {
        iconBottomConstraint?.constant = -UIScreen.height - (iconBottomConstraint?.constant)!
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(HJDefaultDuration, animations: { () -> Void in
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // TODO: 回到首页
                        print("动画完成")
                        AppDelegate.outSwitchRootViewController(true)
                })
        }
    }
    
    // MARK: - lazy load
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 42.5
        image.layer.masksToBounds = true
        return image
    }()
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = "Welcome"
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
}
